﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CoyoEden;
using System.Collections.Generic;
using CoyoEden.Models;

public partial class User_controls_CategorySelector : System.Web.UI.UserControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			//css
			Utils.AddStylesheetInclude(Utils.RelativeWebRoot + "assets/jquerythemes/flora/flora.accordion.css", Page, false);
		}
		if (!Page.IsCallback) {
			categorySelector.Categories = CategoryList;
		}
	}

	#region private fields
	private static readonly string CATEGORIESCACHEKEY = "CategoryListForMenu";
	private static List<Category> _CategoryList;
	private static object _SyncRoot = new object();
	private static List<Category> CategoryList
	{
		get
		{
			_CategoryList = HttpRuntime.Cache[CATEGORIESCACHEKEY] as List<Category>;
			if (_CategoryList == null)
			{
				lock (_SyncRoot)
				{
					if (null == _CategoryList)
					{
						_CategoryList = Category.TopCategories;
						HttpRuntime.Cache.Insert(CATEGORIESCACHEKEY, _CategoryList);
					}
				}
			}
			return _CategoryList;
		}
	}
	#endregion

	#region Category.Saved event subscribing
	static User_controls_CategorySelector()
	{
		Category.Saved += new EventHandler<SavedEventArgs>(Category_Saved);
	}

	static void Category_Saved(object sender, SavedEventArgs e) {
		HttpRuntime.Cache.Remove(CATEGORIESCACHEKEY);
	}
	#endregion
}
